package com.mspbots.web;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.mspbots.api.client.CoreClient;
import com.mspbots.api.client.ReportsClient;
import com.mspbots.api.client.WiseClient;
import org.springframework.amqp.rabbit.core.RabbitMessagingTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.connection.ReactiveRedisConnectionFactory;
import org.springframework.data.redis.core.ReactiveRedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializationContext;
import org.springframework.data.redis.serializer.StringRedisSerializer;

/**
 * com.mspbots.web.BaseService
 *
 * @author Alex bob(https://github.com/vnobo)
 * @date Created by 2019/9/18
 */
public abstract class BaseService {

    protected ObjectMapper objectMapper;
    protected CoreClient coreClient;
    protected ReactiveRedisTemplate<String, Object> reactiveRedisTemplate;
    protected WiseClient wiseClient;
    protected ReportsClient reportsClient;
    protected RabbitMessagingTemplate messagingTemplate;


    @Autowired
    public void setObjectMapper(ObjectMapper objectMapper) {
        this.objectMapper = objectMapper;
    }

    @Autowired
    public void setCoreClient(CoreClient coreClient) {
        this.coreClient = coreClient;
    }

    @Autowired
    public void setWiseClient(WiseClient wiseClient) {
        this.wiseClient = wiseClient;
    }

    @Autowired
    public void setReportsClient(ReportsClient reportsClient) {
        this.reportsClient = reportsClient;
    }

    @Autowired
    public void setReactiveRedisTemplate(ReactiveRedisConnectionFactory factory, ObjectMapper objectMapper) {
        StringRedisSerializer keySerializer = new StringRedisSerializer();
        GenericJackson2JsonRedisSerializer valueSerializer = new GenericJackson2JsonRedisSerializer(objectMapper);
        RedisSerializationContext.RedisSerializationContextBuilder<String, Object> builder =
                RedisSerializationContext.newSerializationContext(keySerializer);
        RedisSerializationContext<String, Object> context = builder.value(valueSerializer).build();
        this.reactiveRedisTemplate = new ReactiveRedisTemplate<>(factory, context);
    }

    @Autowired
    public void setMessagingTemplate(RabbitMessagingTemplate messagingTemplate) {
        this.messagingTemplate = messagingTemplate;
    }


}
